/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.cms.web;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.JsonResult;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.ReadExcel;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.cms.entity.Record_product;
import com.thinkgem.jeesite.modules.cms.entity.Record_summary;
import com.thinkgem.jeesite.modules.cms.entity.Record_task;
import com.thinkgem.jeesite.modules.cms.entity.TaskOperation;
import com.thinkgem.jeesite.modules.cms.entity.dayCheckUserid;
import com.thinkgem.jeesite.modules.cms.entity.dayProgress;
import com.thinkgem.jeesite.modules.cms.entity.taskData;
import com.thinkgem.jeesite.modules.cms.service.RecordService_product;
import com.thinkgem.jeesite.modules.cms.service.RecordService_summary;
import com.thinkgem.jeesite.modules.cms.service.RecordService_task;
import com.thinkgem.jeesite.modules.cms.service.TaskGroupService;
import com.thinkgem.jeesite.modules.cms.service.TaskOperationService;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.thinkgem.jeesite.modules.sys.utils.LogUtils;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

// TODO: Auto-generated Javadoc
/**
 * 子任务管理Controller.
 *
 * @author leo
 * @version 2017-02-16
 */
@Controller
@RequestMapping(value = "${adminPath}/cms/taskOperation")
public class TaskOperationController extends BaseController {

	/** The task operation service. */
	@Autowired
	private TaskOperationService taskOperationService;
	
	/** The record task service. */
	@Autowired
	private RecordService_task record_taskService;
	
	/** The record product service. */
	@Autowired
	private RecordService_product record_productService;
	
	/** The record summary service. */
	@Autowired
	private RecordService_summary record_summaryService;
	
	/** The task group service. */
	@Autowired
	private TaskGroupService taskGroupService;
	
	/** The system service. */
	@Autowired
	private SystemService systemService;
	
	/**
	 * Gets the.
	 *
	 * @param id the id
	 * @return the task operation
	 */
	@ModelAttribute
	public TaskOperation get(@RequestParam(required=false) String id) {
		TaskOperation entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = taskOperationService.get(id);
		}
		if (entity == null){
			entity = new TaskOperation();
		}
		return entity;
	}
	
	
	/**
	 * List.
	 *
	 * @param taskOperation the task operation
	 * @param request the request
	 * @param response the response
	 * @param model the model
	 * @return the string
	 */
	@RequiresPermissions("cms:taskOperation:view")
	@RequestMapping(value = {"list"})
	public String list(TaskOperation taskOperation, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TaskOperation> page = taskOperationService.findPage2(new Page<TaskOperation>(request, response), taskOperation); 
		List<User> userList =  systemService.findAllInUser();
		model.addAttribute("page", page);
		model.addAttribute("userList", userList);
		return "modules/cms/taskOperationList";
	}
	
	@RequiresPermissions("cms:taskOperation:view")
	@RequestMapping(value = {""})
	public String list2(TaskOperation taskOperation, HttpServletRequest request, HttpServletResponse response, Model model) {
//		Page<TaskOperation> page = taskOperationService.findPage(new Page<TaskOperation>(request, response), taskOperation); 
//		List<User> userList =  systemService.findAllInUser();
//		model.addAttribute("page", page);
//		model.addAttribute("userList", userList);
		return "modules/cms/taskOperationList";
	}

	/**
	 * Form.
	 *
	 * @param taskOperation the task operation
	 * @param model the model
	 * @return the string
	 */
	@RequiresPermissions("cms:taskOperation:view")
	@RequestMapping(value = "form")
	public String form(TaskOperation taskOperation, Model model) {
		model.addAttribute("taskOperation", taskOperation);
		return "modules/cms/taskOperationForm";
	}

	/**
	 * Save.
	 *
	 * @param taskOperation the task operation
	 * @param model the model
	 * @param redirectAttributes the redirect attributes
	 * @return the string
	 */
	@RequiresPermissions("cms:taskOperation:edit")
	@RequestMapping(value = "save")
	public String save(TaskOperation taskOperation, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, taskOperation)){
			return form(taskOperation, model);
		}
		taskOperationService.save(taskOperation);
		addMessage(redirectAttributes, "保存信息成功");
		return "redirect:"+Global.getAdminPath()+"/cms/taskOperation/?repage";
	}
	
	/**
	 * Preclear.
	 *
	 * @return the string
	 */
	@RequestMapping({ "preclear" })
	public String preclear(){
		return "modules/cms/taskOperationClear";
	}
	
	/**
	 * Clear.
	 *
	 * @param paramMap the param map
	 * @param model the model
	 * @param request the request
	 * @return the string
	 */
	@RequestMapping({ "clear" })
	public String clear(@RequestParam Map<String, Object> paramMap,
			Model model, HttpServletRequest request) {
		Date endDate = DateUtils.parseDate(paramMap.get("endDate"));
		if (endDate == null) {
			model.addAttribute("message", "请选择终止日期");
			return "modules/cms/taskOperationClear";
		}
		String end = DateUtils.formatDate(endDate,
				new Object[] { "yyyy-MM-dd" });
		this.taskOperationService.clear(end);
		model.addAttribute("message", "删除成功");
		LogUtils.saveLog(request, "删除" + end + "数据");
		paramMap.put("endDate", end);
		model.addAttribute("paramMap", paramMap);
		return "modules/cms/taskOperationClear";
	}
	
	/**
	 * Delete.
	 *
	 * @param taskOperation the task operation
	 * @param redirectAttributes the redirect attributes
	 * @return the string
	 */
	@RequiresPermissions("cms:taskOperation:delete")
	@RequestMapping(value = "delete")
	public String delete(TaskOperation taskOperation, RedirectAttributes redirectAttributes) {
		if(taskOperation.getIssalary())
		{
			addMessage(redirectAttributes, "任务结算不能删除");
			 
		}else{
			if (taskOperation.getTasktype().trim().equals("J")){
				if(taskOperation.getUserid()=="" ||taskOperation.getUserid()== null )//taskOperation.getIsstart() || taskOperation.getIstaskcomplete()
				{
					taskOperationService.delete(taskOperation);
					addMessage(redirectAttributes, "删除信息成功");
					//return "redirect:"+Global.getAdminPath()+"/cms/taskOperation/?repage";
					
				}else{
					addMessage(redirectAttributes, "已分配用户id不能删除");
					//return "modules/cms/taskOperationList";
				}
			}else if (taskOperation.getTasktype().trim().equals("B")){
				if(taskOperation.getIsstart()  ==false ) 
				{
					taskOperationService.delete(taskOperation);
					addMessage(redirectAttributes, "删除信息成功");
				}else{//补时 开工之后不能删除
					addMessage(redirectAttributes, "补时任务已开始不能删除");
				}
				
			}
		}
		
		return "redirect:"+Global.getAdminPath()+"/cms/taskOperation/?repage";//list
	}
	
	/**
	 * Select list.
	 *
	 * @param taskOperation the task operation
	 * @param request the request
	 * @param response the response
	 * @param model the model
	 * @return the string
	 */
	@RequestMapping({ "selectList" })
	public String selectList(TaskOperation taskOperation, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		list(taskOperation, request, response, model);
		return "modules/cms/taskSelectList";
	}

	/**
	 * Insert.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the json result
	 */
	@RequestMapping(value = { "/insert" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public JsonResult insert(HttpServletRequest request,HttpServletResponse response) {
		JsonResult result = new JsonResult();

		String keyfield = request.getParameter("keyfield");
		if ((keyfield == null) || (keyfield.equals(""))) {
			result.setRet_code(-1);
			result.setErr_msg("缺少关键字段keyfield");
		} else {
			TaskOperation taskOperation = this.taskOperationService.getByKeyfield2(keyfield);
			if (taskOperation != null) {
				result.setRet_code(-1);
				result.setErr_msg("主键重复");
			} else {
				Enumeration enu = request.getParameterNames();
				TaskOperation data = new TaskOperation();
				while (enu.hasMoreElements()) {
					String paraName = (String) enu.nextElement();
					getParamValue(data, paraName,
							request.getParameter(paraName));
				}
				this.taskOperationService.save(data);
				result.setRet_code(0);
				TaskOperation task2 = this.taskOperationService.getByKeyfield2(keyfield);
				result.setData(task2);
			}
		}
		return result;
	}

	/**
	 * Gets the param value.
	 *
	 * @param task the task
	 * @param paramKey the param key
	 * @param value the value
	 * @return the param value
	 */
	private void getParamValue(TaskOperation task, String paramKey, String value) {
		if (paramKey.equals("keyfield")) {
			task.setKeyfield(value);
		}
		if (paramKey.equals("productid")) {
			task.setProductid(value);
		}
		if (paramKey.equals("productname")) {
			task.setProductname(value);
		}
		if (paramKey.equals("opdesc")) {
			task.setOpdesc(value);
		}
		if (paramKey.equals("dptask")) {
			task.setDptask(value);
		}
		if (paramKey.equals("dept")) {
			task.setDept(value);
		}
		if (paramKey.equals("pdate")) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			try {
				Date pdate = sdf.parse(value);
				task.setPdate(pdate);
				task.setPdateYear(pdate.getYear());
				task.setPdateMonth(pdate.getMonth());
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		if (paramKey.equals("enddate")) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			try {
				Date pdate = sdf.parse(value);
				task.setEnddate(pdate);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		if (paramKey.toUpperCase().equals("A")) {
			task.setA(Double.parseDouble(value));
		}
		if (paramKey.toUpperCase().equals("B")) {
			task.setB(Double.parseDouble(value));
		}
		if (paramKey.toUpperCase().equals("C")) {
			task.setC(Double.parseDouble(value));
		}
		if (paramKey.toUpperCase().equals("D")) {
			task.setD(Double.parseDouble(value));
		}
		if (paramKey.toUpperCase().equals("E")) {
			task.setE(Double.parseDouble(value));
		}
		if (paramKey.toUpperCase().equals("F")) {
			task.setF(Double.parseDouble(value));
		}
		if (paramKey.toUpperCase().equals("G")) {
			task.setG(Double.parseDouble(value));
		}
		if (paramKey.toUpperCase().equals("H")) {
			task.setH(Double.parseDouble(value));
		}
		if (paramKey.equals("tasktype")) {
			task.setTasktype(value);
		}
		if (paramKey.equals("istaskcomplete")) {
			task.setIstaskcomplete(value.toLowerCase().equals("true"));
		}
		if (paramKey.equals("istagprocess")) {
			task.setIstagprocess(value.toLowerCase().equals("true"));
		}
		if (paramKey.equals("ispartcomplete")) {
			task.setIspartcomplete(value.toLowerCase().equals("true"));
		}
		if (paramKey.equals("isqctask")) {
			task.setIsqctask(value.toLowerCase().equals("true"));
		}
		if (paramKey.equals("isok")) {
			task.setIsok(value.toLowerCase().equals("true"));
		}
		if (paramKey.equals("isstart")) {
			task.setIsstart(value.toLowerCase().equals("true"));
		}
		if (paramKey.equals("userid")) {
			task.setUserid(value);
		}
		if (paramKey.equals("logcomment")) {
			task.setLogcomment(value);
		}
		if (paramKey.equals("jobgroupid")) {
			task.setJobgroupid(value);
		}
		if (paramKey.equals("character01")) {
			task.setCharacter01(value);
		}
		if (paramKey.equals("character02")) {
			task.setCharacter02(value);
		}
		if (paramKey.equals("character03")) {
			task.setCharacter03(value);
		}
		if (paramKey.equals("character04")) {
			task.setCharacter04(value);
		}
		if (paramKey.equals("character05")) {
			task.setCharacter05(value);
		}
		if (paramKey.equals("character06")) {
			task.setCharacter06(value);
		}
		if (paramKey.equals("partnum")) {
			task.setPartnum(value);
		}
		if (paramKey.equals("commenttext")) {
			task.setCommenttext(value);
		}
		if (paramKey.equals("opcode")) {
			task.setOpcode(value);
		}
		if (paramKey.equals("jcdept")) {
			task.setJcdept(value);
		}
		if (paramKey.equals("prodqty")) {
			task.setProdqty(Double.parseDouble(value));
		}
		if (paramKey.equals("number03")) {
			task.setNumber03(value);
		}
		if (paramKey.equals("number07")) {
			task.setNumber07(value);
		}
		if (paramKey.equals("number13")) {
			task.setNumber13(value);
		}
		if (paramKey.equals("starhour")) {
			task.setStarhour(value);
		}
		if (paramKey.equals("prodcode")) {
			task.setProdcode(value);
		}
		if (paramKey.equals("xpartnum")) {
			task.setXpartnum(value);
		}
	}

	/**
	 * Upload.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the string
	 */
	@RequestMapping({ "upload" })
	public String upload(HttpServletRequest request,
			HttpServletResponse response) {
		MultipartHttpServletRequest mulRequest = (MultipartHttpServletRequest) request;
		MultipartFile file = mulRequest.getFile("filename");
		String filename = file.getOriginalFilename();
		if ((filename == null) || ("".equals(filename))) {
			return null;
		}
		String path = request.getSession().getServletContext()
				.getRealPath("upload/excel");
		File targetFile = new File(path, filename);
		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}
		try {
			file.transferTo(targetFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			List<List<Object>> objs = ReadExcel.readExcel(targetFile);
			for (int i = 1; i < objs.size(); i++) {
				TaskOperation data = new TaskOperation();
				data.setKeyfield(((List) objs.get(i)).get(0).toString());
				data.setProductid(((List) objs.get(i)).get(1).toString());
				data.setProductname(((List) objs.get(i)).get(2).toString());
				data.setOpdesc(((List) objs.get(i)).get(3).toString());
				data.setDptask(((List) objs.get(i)).get(4).toString());
				data.setDept(((List) objs.get(i)).get(5).toString());
				SimpleDateFormat sdf = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");
				SimpleDateFormat sdf_year = new SimpleDateFormat("yyyy");
				Date pdate = sdf.parse(((List) objs.get(i)).get(6).toString());
				Calendar mCalendar = Calendar.getInstance();
				mCalendar.setTime(pdate);
				data.setPdate(pdate);
				data.setProdqty(Double.parseDouble(((List) objs.get(i)).get(7).toString()));
				data.setPdateYear(Integer.parseInt(sdf_year.format(pdate)));
				data.setPdateMonth(mCalendar.get(2) + 1);
				data.setA(Double.parseDouble(((List) objs.get(i)).get(8).toString()));
				data.setB(Double.parseDouble(((List) objs.get(i)).get(8+1).toString()));
				data.setC(Double.parseDouble(((List) objs.get(i)).get(8+2).toString()));
				data.setD(Double.parseDouble(((List) objs.get(i)).get(8+3).toString()));
				data.setE(Double.parseDouble(((List) objs.get(i)).get(8+4).toString()));
				data.setF(Double.parseDouble(((List) objs.get(i)).get(8+5).toString()));
				data.setG(Double.parseDouble(((List) objs.get(i)).get(8+6).toString()));
				data.setH(Double.parseDouble(((List) objs.get(i)).get(8+7).toString()));
				data.setTasktype(((List) objs.get(i)).get(9+7).toString());
				data.setIstaskcomplete(Boolean.parseBoolean(((List) objs.get(i))
						.get(10+7).toString()));
				data.setIstagprocess(Boolean.parseBoolean(((List) objs.get(i))
						.get(11+7).toString()));
				data.setIspartcomplete(Boolean.parseBoolean(((List) objs.get(i))
						.get(12+7).toString()));
				data.setIsqctask(Boolean.parseBoolean(((List) objs.get(i)).get(
						13+7).toString()));
				data.setIsok(Boolean.parseBoolean(((List) objs.get(i)).get(14+7)
						.toString()));
				data.setIsstart(Boolean.parseBoolean(((List) objs.get(i)).get(
						15+7).toString()));
				data.setUserid(((List) objs.get(i)).get(16+7).toString());
				data.setCharacter01(((List) objs.get(i)).get(17+7).toString());
				data.setCharacter02(((List) objs.get(i)).get(18+7).toString());
				data.setCharacter03(((List) objs.get(i)).get(19+7).toString());
				data.setCharacter04(((List) objs.get(i)).get(20+7).toString());
				data.setCharacter05(((List) objs.get(i)).get(21+7).toString());
				data.setCharacter06(((List) objs.get(i)).get(22+7).toString());
				data.setPartnum(((List) objs.get(i)).get(23+7).toString());
				data.setCommenttext(((List) objs.get(i)).get(24+7).toString());
				data.setProdcode(((List) objs.get(i)).get(25+7).toString());
				data.setJcdept(((List) objs.get(i)).get(26+7).toString());
				data.setNumber03(((List) objs.get(i)).get(27+7).toString());
				data.setNumber07(((List) objs.get(i)).get(28+7).toString());
				data.setNumber13(((List) objs.get(i)).get(29+7).toString());
				data.setOpcode(((List) objs.get(i)).get(30+7).toString());
				data.setStarhour(((List) objs.get(i)).get(31+7).toString());
				data.setXpartnum(((List) objs.get(i)).get(32+7).toString());

				this.taskOperationService.save(data);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:" + this.adminPath + "/cms/taskOperation";
	}

	/**
	 * Gets the no complete.
	 *
	 * @return the no complete
	 */
	@RequestMapping(value = { "/getNoComplete" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public JsonResult getNoComplete() {
		JsonResult result = new JsonResult();
		List<TaskOperation> tasks = this.taskOperationService.getNoEnd();
		result.setData(tasks);
		if (tasks != null) {
			result.setRet_code(0);
		} else {
			result.setRet_code(-1);
		}
		return result;
	}

	/**
	 * Update complete.
	 *
	 * @param keyfield the keyfield
	 * @return the json result
	 */
	@RequestMapping(value = { "/updateComplete" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public JsonResult updateComplete(String keyfield) {
		JsonResult result = new JsonResult();

		int count = this.taskOperationService.partcomplete(keyfield);

		result.setRet_code(0);
		result.setErr_msg(count > 0 ? "" : "更新条数0");
		return result;
	}

	/**
	 * Gets the task 2.
	 *
	 * @param keyfield the keyfield
	 * @return the task 2
	 */
	@RequestMapping(value = { "/getTask2" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public JsonResult getTask2(String keyfield) {
		JsonResult result = new JsonResult();
		TaskOperation task = this.taskOperationService.getByKeyfield2(keyfield);
		if (task.getIsok() ==false && task.getIsstart() == false) {
			task.setIsok(this.taskOperationService.getProdOk(task
					.getProductid()));
		}
		result.setData(task);
		if (task != null) {
			result.setRet_code(0);
		} else {
			result.setRet_code(-1);
		}
		return result;
	}

	
	/**
	 * Gets the task.
	 *
	 * @param keyfield the keyfield
	 * @return the task
	 */
	@RequestMapping(value = { "/getTask" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public JsonResult getTask(String keyfield) {
		JsonResult result = new JsonResult();
		TaskOperation task = this.taskOperationService.getByKeyfield2(keyfield);
		if (task != null) {
			result.setData(task);
			result.setRet_code(0);
		} else {
			result.setRet_code(-1);
		}
		return result;
	}
	
	/**
	 * Gets the task.
	 *
	 * @param keyfield the keyfield
	 * @return the task
	 */
	@RequestMapping(value = { "/getTaskCK" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public JsonResult getTaskCK(String keyfield) {
		JsonResult result = new JsonResult();
		TaskOperation task = this.taskOperationService.getByKeyfield2(keyfield);
		if (task != null) {
			
			result.setData(task);
			result.setRet_code(0);
		} else {
			//if (task.getIsok() ==false && task.getIsstart() == false) {
			//	task.setIsok(this.taskOperationService.getProdOk(task
			//			.getProductid()));
			//}
			String pid = keyfield.substring(0,keyfield.indexOf('/'))+keyfield.substring(keyfield.lastIndexOf('/'));
			if (this.taskOperationService.getProdOk(pid)) {
				result.setRet_code(-2);//合格  true
			}else{
				result.setRet_code(-1);//不合格  false
			}
			
		}
		return result;
	}
	
	
	
	/**
	 * 获取产品id的可登记数据  
	 * 限制条件有：产品id 用户id为空   登入用户的jobgroupid jcdeptid
	 *
	 * @param productid the productid
	 * @param userid the userid
	 * @return the tas by PN
	 */
	@RequestMapping(value = { "/getTasByPN" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public JsonResult getTastByPN(String productid,String userid) {
		JsonResult result = new JsonResult();
		List<taskData> task = this.taskOperationService.getTastByPN(productid,userid);
		
		result.setData(task);
		if (task != null) {
			result.setRet_code(0);
		} else {
			result.setRet_code(-1);
		}
		return result;
	}
	
	/**
	 * 登记 查询数据
	 * 获取产品id的可报工开始和结束的数据  
	 * 限制条件有：产品id 用户id   任务未完成istaskcomp=false  产品是否合格 isok=false(合格) true(不合格)  
	 * 已登记有用户 未完成 没被冻结 ispartcompl
	 * 
	 * @param productid the productid
	 * @param userid the userid
	 * @return the tas by PN
	 */
	@RequestMapping(value = { "/getByPNAndT" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public JsonResult getByPNAndTcompl(String productid,String userid) {
		JsonResult result = new JsonResult();
		List<taskData> task = this.taskOperationService.getByPNAndTcompl(productid,userid);
		
		result.setData(task);
		if (task != null) {
			result.setRet_code(0);
		} else {
			result.setRet_code(-1);
		}
		return result;
	}
	
	
	@RequestMapping(value = { "/checkindata" })
	@ResponseBody
	public JsonResult checkindata(String query1) {
		JsonResult result = new JsonResult();
		String userid;String keyfieldid;
		String[] s = query1.split(",");
		userid = s[0];
		keyfieldid = s[1];
		result.setData("保存成功");
		if (keyfieldid !=  null && userid != null && keyfieldid.isEmpty() == false && userid.isEmpty() == false) {
			TaskOperation taskOperation = this.taskOperationService.getByKeyfield2(keyfieldid);
			taskOperation.setUserid(userid);
			taskOperationService.save(taskOperation);
			Record_task record = new Record_task();
			Date date = new Date();

			record.setKeyfield(taskOperation.getKeyfield());
			record.setDate(date);
			record.setTime(date);
			record.setType("D");
			record.setUserid(userid);
			record.setDescription("登记"+userid);
			this.record_taskService.save(record);
			result.setRet_code(0);
		} else {
			result.setRet_code(-1);
		}
		return result;
	}
	

	/**
	 * Gets the product.
	 *
	 * @param productid the productid
	 * @return the product
	 */
	@RequestMapping(value = { "/getProduct" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public JsonResult getProduct(String productid) {
		JsonResult result = new JsonResult();
		taskData task = this.taskOperationService.getByProductid(productid);
		result.setData(task);
		if (task != null) {
			result.setRet_code(0);
		} else {
			result.setRet_code(-1);
			result.setErr_msg("没有数据!");
		}
		return result;
	}
	
	@RequestMapping(value = { "/getByProdid" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public JsonResult getByProdid(String productid) {
		JsonResult result = new JsonResult();
		List<taskData> list = this.taskOperationService.getByProdid(productid);
		result.setData(list);
		if (list != null) {
			result.setRet_code(0);
		} else {
			result.setRet_code(-1);
		}
		return result;
	}
	
	@RequestMapping(value = { "/getByProdid2" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public JsonResult getByProdid2(String productid) {
		JsonResult result = new JsonResult();
		List<taskData> list = this.taskOperationService.getByProdid2(productid);
		result.setData(list);
		if (list != null) {
			result.setRet_code(0);
		} else {
			result.setRet_code(-1);
		}
		return result;
	}
	/**
	 * Start.
	 *
	 * @param keyfield the keyfield
	 * @param userid the userid
	 * @param force the force
	 * @return the json result
	 */
	@RequestMapping(value = { "/start" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public JsonResult start(String keyfield, String userid, int force) {
		JsonResult result = new JsonResult();
		result.setData(keyfield);
		if (force == 0) {
			TaskOperation td = this.taskOperationService.getByKeyfield2(keyfield);
			if(td.getIstaskcomplete()){
				result.setRet_code(2);
				result.setErr_msg("记录已完工");
				return result;
			}
			if (td.getIsstart()) {
				result.setRet_code(1);
				result.setErr_msg("有开工记录，是否需要继续开工?");
			} else {
				this.taskOperationService.start(keyfield, userid);
				Record_task record = new Record_task();
				Date date = new Date();

				record.setKeyfield(keyfield);
				record.setDate(date);
				record.setTime(date);
				record.setType("S");
				record.setUserid(userid);
				record.setDescription(userid + "-开工");
				this.record_taskService.save(record);

				result.setRet_code(0);
			}
		} else {
			this.taskOperationService.start(keyfield, userid);
			Record_task record = new Record_task();
			Date date = new Date();

			record.setKeyfield(keyfield);
			record.setDate(date);
			record.setTime(date);
			record.setType("S");
			record.setUserid(userid);
			record.setDescription(userid + "-开工");
			this.record_taskService.save(record);

			result.setRet_code(0);
		}
		return result;
	}

	/**
	 * Complete.
	 *
	 * @param keyfield the keyfield
	 * @param userid the userid
	 * @param update the update
	 * @return the json result
	 */
	@RequestMapping(value = { "/complete" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public JsonResult complete(String keyfield, String userid, int update) {
		JsonResult result = new JsonResult();
		result.setData(keyfield);
		TaskOperation td = this.taskOperationService.getByKeyfield2(keyfield);
		if (!td.getIsstart()) {
			result.setRet_code(1);
			result.setErr_msg("记录还未开工");
		} else {
			if (td.getIstaskcomplete())
			{
				result.setRet_code(2);
				result.setErr_msg("记录已完工");
				return result;
			}
			if (update == 1) {
				this.taskOperationService.complete(keyfield, userid);
			} else {
				this.taskOperationService.qcComplete(keyfield);
			}
			Record_task record = new Record_task();
			Date date = new Date();

			record.setKeyfield(keyfield);
			record.setDate(date);
			record.setTime(date);
			record.setType("E");
			record.setUserid(userid);
			record.setDescription(userid + "-完工");
			this.record_taskService.save(record);

			result.setRet_code(0);
		}
		return result;
	}
	
	@RequestMapping(value = { "/prodcomplete" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public JsonResult prodcomplete(String productid, String userid, int update) {
		JsonResult result = new JsonResult();
		result.setData(productid);
		TaskOperation td = this.taskOperationService.getByKeyfield2(productid);
		if (!td.getIsstart()) {
			result.setRet_code(1);
			result.setErr_msg("记录还未开工");
		} else {
			if (update == 1) {
				this.taskOperationService.productcomplete(productid, userid);
			} else {
				this.taskOperationService.qcComplete(productid);
			}
			Record_task record = new Record_task();
			Date date = new Date();

			record.setKeyfield(productid);
			record.setDate(date);
			record.setTime(date);
			record.setType("E");
			record.setUserid(userid);
			record.setDescription(userid + "-完工");
			this.record_taskService.save(record);

			result.setRet_code(0);
		}
		return result;
	}

	/**
	 * Uploadrecord.
	 *
	 * @param keyfield the keyfield
	 * @param userid the userid
	 * @param src the src
	 * @return the json result
	 */
	@RequestMapping(value = { "/uploadrecord" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public JsonResult uploadrecord(String keyfield, String userid, String src) {
		JsonResult result = new JsonResult();
		Record_task record = new Record_task();
		Date date = new Date();

		record.setKeyfield(keyfield);
		record.setDate(date);
		record.setTime(date);
		record.setType("P");//未启用
		record.setUserid(userid);
		record.setDescription(src);
		this.record_taskService.save(record);

		result.setRet_code(0);

		return result;
	}

	/**
	 * Back.
	 *
	 * @param keyfield the keyfield
	 * @param userid the userid
	 * @param reason the reason
	 * @return the json result
	 */
	@RequestMapping(value = { "/back" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public JsonResult back(String keyfield, String userid, String reason,String valuea) {
		JsonResult result = new JsonResult();

		this.taskOperationService.back(keyfield, userid);
		Record_task record = new Record_task();
		Date date = new Date();

		record.setKeyfield(keyfield);
		record.setDate(date);
		record.setTime(date);
		record.setType("QC");
		record.setUserid(userid);
		record.setDescription(splitReason(reason));
		record.setDescription2(valuea);
		this.record_taskService.save(record);

		result.setRet_code(0);

		return result;
	}

	/**
	 * .
	 *没有使用
	 * @param productid the productid
	 * @param coment the reason
	 * @param userid the userid
	 * @return the json result
	 */
	@RequestMapping(value = { "/productQualified" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public JsonResult productQualified(String productid, String coment, String userid) {
		JsonResult result = new JsonResult();

//		this.taskOperationService.updateProduct(productid, 1);
		Record_product record = new Record_product();
		Date date = new Date();

		record.setProductid(productid);
		record.setType("Q");
		record.setDate(date);
		record.setTime(date);
		record.setIsok(1);
		record.setUserid(userid);
		//record.setDescription(splitReason(coment));
		record.setDescription2(coment);
		this.record_productService.save(record);

		result.setRet_code(0);

		return result;
	}
	
	/*
	 * pqc
	 */
	@RequestMapping(value = { "/productok" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public JsonResult productok(String productid, String reason, String userid) {
		JsonResult result = new JsonResult();

		this.taskOperationService.updateProduct(productid, 1);
		Record_product record = new Record_product();
		Date date = new Date();

		record.setProductid(productid);
		record.setType("C");
		record.setDate(date);
		record.setTime(date);
		record.setIsok(1);
		record.setUserid(userid);
		//record.setDescription(splitReason(coment));
		String coment = reason;
		record.setDescription2(coment);
		this.record_productService.save(record);

		result.setRet_code(0);

		return result;
	}
	
	/**
	 * fqc产品合格 只记录操作信息
	 * @param productid
	 * @param reason
	 * @param userid
	 * @return
	 */
	@RequestMapping(value = { "/productok2" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public JsonResult productok2(String productid, String reason, String userid) {
		JsonResult result = new JsonResult();

		//this.taskOperationService.updateProduct(productid, 1);
		if (this.taskOperationService.istaskcompleteByPid(productid) ==false){
			result.setErr_msg("产品工序未完成");
			result.setRet_code(-1);

			return result;
		}
		Record_product record = new Record_product();
		Date date = new Date();

		record.setProductid(productid);
		record.setType("FC");
		record.setDate(date);
		record.setTime(date);
		record.setIsok(1);
		record.setUserid(userid);
		//record.setDescription(splitReason(coment));
		String coment = reason;
		record.setDescription2(coment);
		this.record_productService.save(record);

		result.setRet_code(0);

		return result;
	}

	/**
	 * Productnotok.
	 *pqc
	 * @param productid the productid
	 * @param reason the reason
	 * @param userid the userid
	 * @return the json result
	 */
	@RequestMapping(value = { "/productnotok" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public JsonResult productnotok(String productid, String reason,
			String userid, String comment) {
		JsonResult result = new JsonResult();
		if (this.taskOperationService.hasStart(productid)) {
			this.taskOperationService.updateProduct(productid, 0);
			
			result.setRet_code(0);
		} else {
			result.setRet_code(-1);
			result.setErr_msg("此产品任务已开工,不合格已执行");
		}
		
		Record_product record = new Record_product();
		Date date = new Date();

		record.setProductid(productid);
		record.setType("C");//record.setType("F");//查询(同一类型C)产品id是否合格
		record.setDate(date);
		record.setTime(date);
		record.setIsok(0);
		record.setUserid(userid);
		record.setDescription(splitReason(reason));
		record.setDescription2(comment);
		this.record_productService.save(record);
		
		return result;
	}
	
	public String splitReason(String reason){
		if (reason.length() >0)
		{
			String[]  reasons = reason.split(":");
			return reasons[0];
		}
		return "";
	}
	
	/**
	 * Productnotok.
	 *fqc
	 * @param productid the productid
	 * @param reason the reason
	 * @param userid the userid
	 * @return the json result
	 */
	@RequestMapping(value = { "/productnotok2" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public JsonResult productnotok2(String productid, String reason,
			String userid, String comment) {
		JsonResult result = new JsonResult();
		Record_product record = new Record_product();
		Date date = new Date();

		record.setProductid(productid);
		record.setType("FC");
		record.setDate(date);
		record.setTime(date);
		record.setIsok(0);
		record.setUserid(userid);
		record.setDescription(splitReason(reason));
		record.setDescription2(comment);
		this.record_productService.save(record);
		
		return result;
	}


	/**
	 * Gets the yeardata.
	 *
	 * @param userid the userid
	 * @return the yeardata
	 */
	@RequestMapping(value = { "/getYeardata" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public JsonResult getYeardata(String userid) {
		JsonResult result = new JsonResult();
		if (userid.equals("")) {
			result.setRet_code(-1);
		} else {
			result.setData(this.taskOperationService.getYeardata(userid));
			result.setRet_code(0);
		}
		return result;
	}

	/**
	 * Suredata.
	 *
	 * @param userid the userid
	 * @param month the month
	 * @param count the count
	 * @param gdp the gdp
	 * @return the json result
	 */
	@RequestMapping(value = { "/suredata" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public JsonResult suredata(String userid, int month, int count, double gdp) {
		JsonResult result = new JsonResult();
		if ((userid.equals("")) || (month < 1) || (month > 12)) {
			result.setRet_code(-1);
		} else {
			Record_summary record = new Record_summary();
			long time = System.currentTimeMillis();
			Calendar mCalendar = Calendar.getInstance();
			mCalendar.setTimeInMillis(time);

			Date date = new Date();
			record.setConfirmcount(count);
			record.setConfirmgdp(gdp);
			record.setConfirmyear(mCalendar.get(1));
			record.setConfirmmonth(month);
			record.setDate(date);
			record.setTime(date);
			record.setUserid(userid);
			this.record_summaryService.save(record);

			result.setRet_code(0);
		}
		return result;
	}

	/**
	 * Gets the monthdata.
	 *
	 * @param userid the userid
	 * @return the monthdata
	 */
	@RequestMapping(value = { "/getMonthdata" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public JsonResult getMonthdata(String userid) {
		JsonResult result = new JsonResult();
		if (userid.equals("")) {
			result.setRet_code(-1);
		} else {
			result.setData(this.taskOperationService.getMonthdata(userid));
			result.setRet_code(0);
		}
		return result;
	}

	/**
	 * Gets the daydata.
	 *
	 * @param userid the userid
	 * @return the daydata
	 */
	@RequestMapping(value = { "/getDaydata" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public JsonResult getDaydata(String userid) {
		JsonResult result = new JsonResult();
		if (userid.equals("")) {
			result.setRet_code(-1);
		} else {
			result.setData(this.taskOperationService.getDaydata(userid));
			result.setRet_code(0);
		}
		return result;
	}
	
	
	/**
	 * Merge.
	 *
	 * @param ids the ids:[1,2,3,4,5,6,7,8,9]
	 * @return the string:xiaoming
	 */
	@RequestMapping(value = "megreA")
	@ResponseBody
	public String merge(String ids,String userid, String loginname){
		//String m = ids;
		if (ids != null && userid!= null && ids.length()>0 && userid.length()>0 ){
			
		}else{
			return "传入参数不能空";
		}
		String[] ms = ids.replace("[", "").replace("]", "").split(",");
		String[] values = new String[ms.length];
		for (int i =0; i<ms.length;i++) {
			int start,len  = 0;
			start = ms[i].indexOf(";");
			len = ms[i].length()-start -1 ;
			values[i]= ms[i].substring(start+1,len);
			
		}
		List<TaskOperation> list = taskOperationService.findList(values);
		String opcode ="";
		boolean istagprocess =false;
		Date pdate ;
		String userid2  = "";
		int groupid ;
		int iscount = 0;
		//前台js 也需要判断
		//两条以上合并 if length  
		for (TaskOperation t : list) {
			opcode = t.getOpcode();
			istagprocess = t.getIstagprocess();
			pdate = t.getPdate();
			userid2 = t.getUserid();
			groupid = t.getTaskGroupId();
			//不同工序不允许合并   同一erp工序才可以合并
			//非关键工序和关键工序不允许合并
			//计划日期同一天的任务合并
			//没有分配用户id的合并   有id不可以
			//未合的任务明细
			iscount = 0 ;
			if (groupid > 0 || (userid2 != null &&  userid2.isEmpty() ==false) ) 
			{
				return "合并id或用户id已存在";
			}
			for (TaskOperation t2 : list) {
				if (opcode == t2.getOpcode() && istagprocess == t2.getIstagprocess() && pdate.equals(t2.getPdate())  && userid2 == t2.getUserid() ){
					
				}else{
					
				}
				if (opcode.trim().equals(t2.getOpcode().trim()) ==false   ||istagprocess != t2.getIstagprocess() || pdate.equals(t2.getPdate()) ==false || userid2 != t2.getUserid()){
					iscount = iscount + 1;
				}
				if (iscount>0)
				{
					return "任务不满足合并条件";
				}
				
			}
				
			
		}
		
		int groupId = taskGroupService.saveList(list,userid);
		
		for (TaskOperation taskOperation : list) {
			taskOperation.setTaskGroupId(groupId);
			taskOperation.setUserid(userid);
			taskOperationService.save(taskOperation);
			
			Record_task record = new Record_task();
			Date date = new Date();

			record.setKeyfield(taskOperation.getKeyfield());
			record.setDate(date);
			record.setTime(date);
			record.setType("C");//合并
			record.setUserid(userid);
			record.setDescription("合并"+getUserName(loginname));
			record.setDescription2(String.valueOf(groupId));
			this.record_taskService.save(record);

		}
		
		//return "redirect:"+Global.getAdminPath()+"/cms/taskGroup/?repage";
		//return "redirect:"+Global.getAdminPath()+"/cms/taskOperation/?repage";
		//return "forward:"+Global.getAdminPath()+"/cms/taskOperation/list";
		return "合并id"+String.valueOf(groupId);
	}
	/**
	 * Merge.
	 *
	 * @param ids the ids:[1,2,3,4,5,6,7,8,9]
	 * @return the string:xiaoming
	 */
	@RequestMapping(value = "megre")
	@ResponseBody
	public String merge(String ids,String userid){
		//String m = ids;
		if (ids != null && userid!= null && ids.length()>0 && userid.length()>0 ){
			
		}else{
			return "error";
		}
		String[] ms = ids.replace("[", "").replace("]", "").split(",");
		String[] values = new String[ms.length];
		for (int i =0; i<ms.length;i++) {
			int start,len  = 0;
			start = ms[i].indexOf(";");
			len = ms[i].length()-start -1 ;
			values[i]= ms[i].substring(start+1,len);
			
		}
		List<TaskOperation> list = taskOperationService.findList(values);
		String opcode ="";
		boolean istagprocess =false;
		Date pdate ;
		String userid2  = "";
		int groupid ;
		int iscount = 0;
		//前台js 也需要判断
		//两条以上合并 if length  
		for (TaskOperation t : list) {
			opcode = t.getOpcode();
			istagprocess = t.getIstagprocess();
			pdate = t.getPdate();
			userid2 = t.getUserid();
			groupid = t.getTaskGroupId();
			//不同工序不允许合并   同一erp工序才可以合并
			//非关键工序和关键工序不允许合并
			//计划日期同一天的任务合并
			//没有分配用户id的合并   有id不可以
			//未合的任务明细
			iscount = 0 ;
			if (groupid > 0 || (userid2 != null &&  userid2.isEmpty() ==false) ) 
			{
				return "合并id或用户id已存在";
			}
			for (TaskOperation t2 : list) {
				if (opcode == t2.getOpcode() && istagprocess == t2.getIstagprocess() && pdate.equals(t2.getPdate())  && userid2 == t2.getUserid() ){
					
				}else{
					
				}
				if (opcode.trim().equals(t2.getOpcode().trim()) ==false   ||istagprocess != t2.getIstagprocess() || pdate.equals(t2.getPdate()) ==false || userid2 != t2.getUserid()){
					iscount = iscount + 1;
				}
				if (iscount>0)
				{
					return "error";
				}
				
			}
				
			
		}
		
		int groupId = taskGroupService.saveList(list,userid);
		
		for (TaskOperation taskOperation : list) {
			taskOperation.setTaskGroupId(groupId);
			taskOperation.setUserid(userid);
			taskOperationService.save(taskOperation);
			
			Record_task record = new Record_task();
			Date date = new Date();

			record.setKeyfield(taskOperation.getKeyfield());
			record.setDate(date);
			record.setTime(date);
			record.setType("C");//合并
			record.setUserid(userid);
			record.setDescription("合并"+getUserName(""));
			record.setDescription2(String.valueOf(groupId));
			this.record_taskService.save(record);

		}
		
		//return "redirect:"+Global.getAdminPath()+"/cms/taskGroup/?repage";
		//return "redirect:"+Global.getAdminPath()+"/cms/taskOperation/?repage";
		//return "forward:"+Global.getAdminPath()+"/cms/taskOperation/list";
		return "合并id"+String.valueOf(groupId);
	}
	
	
	private String getUserName(String name){
		try{
			if (name == null || name ==""){
				name =UserUtils.getPrincipal().getLoginName();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return name;
	}
	
	
	@RequestMapping(value = "deletesel")
	@ResponseBody
	public String deletesel(String ids){
		//String m = ids;
		String[] ms = ids.replace("[", "").replace("]", "").split(",");
		String[] values = new String[ms.length];
		for (int i =0; i<ms.length;i++) {
			int start,len  = 0;
			start = ms[i].indexOf(";");
			len = ms[i].length()-start -1 ;
			values[i]= ms[i].substring(start+1,len);
			
		}
		List<TaskOperation> list = taskOperationService.findList(values);
		String opDesc = "";
		//前台js 也需要判断
		//两条以上合并 if length  
		for (TaskOperation taskOperation : list) {
			if (opDesc.contentEquals(taskOperation.getDptask()) == false)
			{
				
				
			}
		}
		
		for (TaskOperation taskOperation : list) {
			if (opDesc.contentEquals(taskOperation.getDptask()) == false)
			{
				if (taskOperation.getTasktype().trim().equals("J")){
					if(taskOperation.getUserid()!=null && taskOperation.getUserid().length()>0 ) //正常任务 有用户id不能删除
					{
						continue;
					}
					taskOperationService.delete(taskOperation);
				}else if (taskOperation.getTasktype().trim().equals("B")){
					if(taskOperation.getIsstart() ) //补时 开工之后不能删除
					{
						continue;
					}
					taskOperationService.delete(taskOperation);
				}
				
				
			}
		}
		
		//return "redirect:"+Global.getAdminPath()+"/cms/taskGroup/?repage";
		//return "redirect:"+Global.getAdminPath()+"/cms/taskOperation/?repage";
		//return "forward:"+Global.getAdminPath()+"/cms/taskOperation/list";
		return "删除完成";
	}
	
	/**
	 * Assign.
	 *
	 * @param ids the ids
	 * @param userid the userid
	 * @return the string
	 */
	@RequestMapping(value = "assignA")
	@ResponseBody
	public String assign(String ids,String userid,String loginname){
		//String m = ids;
		String[] ms = ids.replace("[", "").replace("]", "").split(",");
		String[] values = new String[ms.length];
		for (int i =0; i<ms.length;i++) {
			int start,len  = 0;
			start = ms[i].indexOf(";");
			len = ms[i].length()-start -1 ;
			values[i]= ms[i].substring(start+1,len);
			
		}
		List<TaskOperation> list = taskOperationService.findList(values);
		String opDesc = "";
		//前台js 也需要判断
		//两条以上合并 if length  
		for (TaskOperation taskOperation : list) {
			if (opDesc.contentEquals(taskOperation.getDptask()) == false)
			{
				
				
			}
		}
		
		if (list.size()==1)
		{
			
			if(list.get(0).getUserid()!=null && list.get(0).getUserid().length()>0 )
			{
				return "指派不成功,已存在用户id!";
			}
		}
		for (TaskOperation taskOperation : list) {
			if (opDesc.contentEquals(taskOperation.getDptask()) == false)
			{
				if(taskOperation.getUserid()!=null && taskOperation.getUserid().length()>0 )
				{
					continue;
				}
				taskOperation.setUserid(userid);
				taskOperationService.save(taskOperation);
				
				Record_task record = new Record_task();
				Date date = new Date();

				record.setKeyfield(taskOperation.getKeyfield());
				record.setDate(date);
				record.setTime(date);
				record.setType("D");
				record.setUserid(userid);
				record.setDescription("指派"+getUserName(loginname));
				this.record_taskService.save(record);
			}
		}
		
		//return "redirect:"+Global.getAdminPath()+"/cms/taskGroup/?repage";
		//return "redirect:"+Global.getAdminPath()+"/cms/taskOperation/?repage";
		//return "forward:"+Global.getAdminPath()+"/cms/taskOperation/list";
		return "指派完成";
	}
	/**
	 * Assign.
	 *
	 * @param ids the ids
	 * @param userid the userid
	 * @return the string
	 */
	@RequestMapping(value = "assign")
	@ResponseBody
	public String assign(String ids,String userid){
		//String m = ids;
		String[] ms = ids.replace("[", "").replace("]", "").split(",");
		String[] values = new String[ms.length];
		for (int i =0; i<ms.length;i++) {
			int start,len  = 0;
			start = ms[i].indexOf(";");
			len = ms[i].length()-start -1 ;
			values[i]= ms[i].substring(start+1,len);
			
		}
		List<TaskOperation> list = taskOperationService.findList(values);
		String opDesc = "";
		//前台js 也需要判断
		//两条以上合并 if length  
		for (TaskOperation taskOperation : list) {
			if (opDesc.contentEquals(taskOperation.getDptask()) == false)
			{
				
				
			}
		}
		
		if (list.size()==1)
		{
			
			if(list.get(0).getUserid()!=null && list.get(0).getUserid().length()>0 )
			{
				return "指派不成功,已存在用户id!";
			}
		}
		for (TaskOperation taskOperation : list) {
			if (opDesc.contentEquals(taskOperation.getDptask()) == false)
			{
				if(taskOperation.getUserid()!=null && taskOperation.getUserid().length()>0 )
				{
					continue;
				}
				taskOperation.setUserid(userid);
				taskOperationService.save(taskOperation);
				
				Record_task record = new Record_task();
				Date date = new Date();

				record.setKeyfield(taskOperation.getKeyfield());
				record.setDate(date);
				record.setTime(date);
				record.setType("D");
				record.setUserid(userid);
				record.setDescription("指派"+getUserName(""));
				this.record_taskService.save(record);
			}
		}
		
		//return "redirect:"+Global.getAdminPath()+"/cms/taskGroup/?repage";
		//return "redirect:"+Global.getAdminPath()+"/cms/taskOperation/?repage";
		//return "forward:"+Global.getAdminPath()+"/cms/taskOperation/list";
		return "指派完成";
	}
	
	
	//变更登记
	@RequestMapping(value = "assign2A")
	@ResponseBody
	public String assign2(String ids,String userid,String loginname){
		//String m = ids;
		String[] ms = ids.replace("[", "").replace("]", "").split(",");
		String[] values = new String[ms.length];
		for (int i =0; i<ms.length;i++) {
			int start,len  = 0;
			start = ms[i].indexOf(";");
			len = ms[i].length()-start -1 ;
			values[i]= ms[i].substring(start+1,len);
			
		}
		List<TaskOperation> list = taskOperationService.findList(values);
		String opDesc = "";
		//前台js 也需要判断
		//两条以上合并 if length  
		for (TaskOperation taskOperation : list) {
			if (opDesc.contentEquals(taskOperation.getDptask()) == false)
			{
				
				
			}
		}
		
		for (TaskOperation taskOperation : list) {
			if (opDesc.contentEquals(taskOperation.getDptask()) == false)
			{
				
				taskOperation.setUserid(userid);
				taskOperationService.save(taskOperation);
				
				Record_task record = new Record_task();
				Date date = new Date();

				record.setKeyfield(taskOperation.getKeyfield());
				record.setDate(date);
				record.setTime(date);
				record.setType("D");
				record.setUserid(userid);
				record.setDescription("指派"+getUserName(loginname));
				this.record_taskService.save(record);
			}
		}
		
		//return "redirect:"+Global.getAdminPath()+"/cms/taskGroup/?repage";
		//return "redirect:"+Global.getAdminPath()+"/cms/taskOperation/?repage";
		//return "forward:"+Global.getAdminPath()+"/cms/taskOperation/list";
		return "指派完成";
	}
	
	//变更登记
		@RequestMapping(value = "assign2")
		@ResponseBody
		public String assign2(String ids,String userid){
			//String m = ids;
			String[] ms = ids.replace("[", "").replace("]", "").split(",");
			String[] values = new String[ms.length];
			for (int i =0; i<ms.length;i++) {
				int start,len  = 0;
				start = ms[i].indexOf(";");
				len = ms[i].length()-start -1 ;
				values[i]= ms[i].substring(start+1,len);
				
			}
			List<TaskOperation> list = taskOperationService.findList(values);
			String opDesc = "";
			//前台js 也需要判断
			//两条以上合并 if length  
			for (TaskOperation taskOperation : list) {
				if (opDesc.contentEquals(taskOperation.getDptask()) == false)
				{
					
					
				}
			}
			
			for (TaskOperation taskOperation : list) {
				if (opDesc.contentEquals(taskOperation.getDptask()) == false)
				{
					
					taskOperation.setUserid(userid);
					taskOperationService.save(taskOperation);
					
					Record_task record = new Record_task();
					Date date = new Date();

					record.setKeyfield(taskOperation.getKeyfield());
					record.setDate(date);
					record.setTime(date);
					record.setType("D");
					record.setUserid(userid);
					record.setDescription("指派"+getUserName(""));
					this.record_taskService.save(record);
				}
			}
			
			//return "redirect:"+Global.getAdminPath()+"/cms/taskGroup/?repage";
			//return "redirect:"+Global.getAdminPath()+"/cms/taskOperation/?repage";
			//return "forward:"+Global.getAdminPath()+"/cms/taskOperation/list";
			return "指派完成";
		}
	//限制调剂:任务未完成Istaskcomplete = false   没有开始的任务isstart= false  没有冻结isok=false  [产品是否合格 isok=false(合格) true(不合格)]
	@RequestMapping(value = { "/getByGroupIdStart" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public JsonResult getByGroupIdStart(String groupid,String userid) {
		JsonResult result = new JsonResult();
		List<taskData> task = this.taskOperationService.getByGroupIdStart(groupid,userid);
		
		result.setData(task);
		if (task != null) {
			result.setRet_code(0);
		} else {
			result.setRet_code(-1);
		}
		return result;
	}
	

	//限制调剂:任务未完成Istaskcomplete = false   没有开始的任务isstart= false  没有冻结isok=false  [产品是否合格 isok=false(合格) true(不合格)]
	@RequestMapping(value = { "/getByGroupIdEnd" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public JsonResult getByGroupIdEnd(String groupid) {
		JsonResult result = new JsonResult();
		List<taskData> task = this.taskOperationService.getByGroupIdEnd(groupid);
		
		result.setData(task);
		if (task != null) {
			result.setRet_code(0);
		} else {
			result.setRet_code(-1);
		}
		return result;
	}
	
	
	@RequestMapping(value = { "/getnostartdata" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public JsonResult getnostartdata(String userid) {
		JsonResult result = new JsonResult();
		if (userid.equals("")) {
			result.setRet_code(-1);
		} else {
			//result.setData(this.taskOperationService.getnostartdata(userid));
			result.setData(this.taskOperationService.getnostartdata2(userid));
			result.setRet_code(0);
		}
		return result;
	}
	

	@RequestMapping(value = { "/getisstartnoend" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public JsonResult getisstartnoend(String userid) {
		JsonResult result = new JsonResult();
		if (userid.equals("")) {
			result.setRet_code(-1);
		} else {
			//result.setData(this.taskOperationService.getisstartnoend(userid));
			result.setData(this.taskOperationService.getisstartnoend2(userid));
			result.setRet_code(0);
		}
		return result;
	}
	
	@RequestMapping(value = {"dayCheckUidlist"})
	@ResponseBody
	public List<dayCheckUserid> dayCheckUidData(Date pdate, HttpServletRequest request, HttpServletResponse response, Model model) {
		if (pdate ==null)
		{
			pdate = new Date();
		}
		List<dayCheckUserid> list = taskOperationService.dayCheckUidData(pdate); 
		model.addAttribute("list", list);
		return list;
	}
	@RequestMapping(value = {"dayProgresslist"})
	@ResponseBody
	public List<dayProgress> dayProgressData(Date pdate, HttpServletRequest request, HttpServletResponse response, Model model) {
		if (pdate ==null)
		{
			pdate = new Date();
		}
		List<dayProgress> list = taskOperationService.dayProgressData(pdate); 
		model.addAttribute("list", list);
		return list;
	}
	
	
	@RequestMapping(value = {"getTaskByDateDept"})
	@ResponseBody
	public List<TaskOperation> getTaskByDateDept(String today,String deptid) {
		Date pdate = null;
		pdate = DateTime.parse(today).toDate();
		if (pdate ==null)
		{
			pdate = new Date();
		}
		List<TaskOperation> list = taskOperationService.getTaskByDateDept(pdate,deptid); 
		return list;
	}
	
	@RequestMapping(value = {"addTaskOprEx"})
	@ResponseBody
	public JsonResult addTaskOprEx(String id,String userid,String gdp) {
		JsonResult result = new JsonResult();
		//taskOperationService.addTaskOprEx(id,userid,gdp);
		return result;
	}
	
	/**
	 * Merge.
	 *
	 * @param ids the ids:[1,2,3,4,5,6,7,8,9]
	 * @return the string:xiaoming
	 */
	@RequestMapping(value = "addTaskOprEx2")
	@ResponseBody
	public JsonResult addTaskOprEx2(String ids,String userid){
		JsonResult result = new JsonResult();
		//String m = ids;
		if (ids != null && userid!= null && ids.length()>0 && userid.length()>0 ){
			
		}else{
			result.setErr_msg("传入参数不能空");
			return result;
		}
		String[] ms = ids.replace("[", "").replace("]", "").split(",");
		String[] values = new String[ms.length];
		for (int i =0; i<ms.length;i++) {
			int start,len  = 0;
			start = ms[i].indexOf(";");
			len = ms[i].length()-start -1 ;
			values[i]= ms[i].substring(start+1,len);
			
		}
		List<TaskOperation> list = taskOperationService.findList(values);
		if (list != null && list.size()> 0){
			for(TaskOperation t :list){
				Double gdp =  t.getA() + t.getB() + t.getC() + t.getD() + t.getE() + t.getF() + t.getG()  + t.getH(); 
				taskOperationService.addTaskOprEx(t.getId(),userid,gdp);
				System.out.println(t.getId());
				
			}
		}
		
		result.setErr_msg("success");
		return result;
	}
	
	
	@RequestMapping(value = { "/gettodaydtlbyuserid" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public JsonResult gettodaydtlbyuserid(String today,String userid) {
		JsonResult result = new JsonResult();
		if (userid.equals("")) {
			result.setRet_code(-1);
		} else {
			result.setData(this.taskOperationService.gettodaydtlbyuserid(today,userid));
			result.setRet_code(0);
		}
		return result;
	}
	
}